﻿using RomanJendrusz.Amazon.ETL;
using RomanJendrusz.Amazon.ETL.Infrastructure.IoC;
using RomanJendrusz.Amazon.ETL.Infrastructure.DataBase;
using RomanJendrusz.Amazon.ETL.Infrastructure.Bus;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;
using System;
using System.Diagnostics;
using RomanJendrusz.Amazon.ETL.Strategies;
using System.Collections.Generic;
using RomanJendrusz.Amazon.ETL.Domain;

namespace ETLRunner
{
    internal class Program
    {
        private static bool RebuildDatabase;
        private static string[] ISBNs;

        private static bool extractAppArgs(string[] args)
        {
            var isbns = new List<string>();
            bool areIsbns = false;
            foreach (var arg in args)
            {
                if(arg.StartsWith(@"\isbns:", StringComparison.OrdinalIgnoreCase))
                {
                    var isbn = arg.TrimStart(@"\isbn:".ToCharArray());
                    if(string.IsNullOrEmpty(isbn.Trim()) == false)
                    {
                        isbns.Add(isbn);
                    }
                    areIsbns = true;
                }
                else if(arg.StartsWith(@"\rebuildDB", StringComparison.OrdinalIgnoreCase))
                {
                    areIsbns = false;
                    RebuildDatabase = true;
                }
                else if(areIsbns)
                {
                    isbns.Add(arg.Trim());
                }
            }
            ISBNs = isbns.ToArray();
            return isbns.Count > 0;
        }

        private static void Main(string[] args)
        {
            if(extractAppArgs(args) == false)
            {
                Console.WriteLine("The aruments are invalid");
                return;
            }
            initialize();
            InitDataBase(RebuildDatabase);
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ETLConfiguration.BeginETL();
            try
            {
                var strategy = IoCGateway.Resolve<IDataRetrievalStrategy>();
                strategy.RetrieveBy(ISBNs);
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Beep();
                Console.WriteLine("\r\n\r\nAn ERROR occured ...................");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine("\r\n");
                Console.ResetColor();
            }
            finally
            {
                watch.Stop();
                ETLConfiguration.EndETL();
                Console.WriteLine("Amazon ETL has ended after : " + watch.Elapsed + " ........");
                Console.WriteLine("Total books retrieved: " + AmazonCounters.BooksRetrieved);
                Console.WriteLine("Total books checked for similiarity: " + AmazonCounters.BooksCheckedForSimiliarity);
                Console.WriteLine("Total customers reviews retrived: " + AmazonCounters.CustomerReviewsRetrieved);
                Console.WriteLine("Total items that where not books: " + AmazonCounters.ItemsThatAreNotBooks);
                Console.Read();
            }
        }

        private static void initialize()
        {
            AmazonETL.Initialize();
            LogGateway.InitializeWith(IoCGateway.Resolve<ILogFactory>());
            var ecsFactory = IoCGateway.Resolve<AmazonECSFactory>();
            ecsFactory.AddAwsUserKey("1FSSNYS4T9YMWMNCBQG2");
        }

        private static void InitDataBase(bool rebuild)
        {
            var db = IoCGateway.Resolve<DataBaseManager>();
            db = IoCGateway.Resolve<DataBaseManager>();
            if(rebuild)
            {
                db.RebuildDatabase();
            }
            else
            {
                if (db.DatabaseExists == false)
                {
                    db.CreateDatabase();
                }    
            }
            
        }
    }
}